// pages/applyforrent/applyforrent.js
var _tip = require('../../utils/tip.js');
var _check = require('../../utils/check.js');
var _swit = require('../../utils/swit.js');
Page({
  data: {
    tipUrl: '',
    placeholderColor: '#d7d7d7',
    verificationCodeButtonText: '获取验证码',
    isPressSendVerificationCode: false,
    currentMaskId: '',
    isShowMask: false,
    isAgree: false,
    creditbanks: ['中国银行', '农业银行', '工商银行', '交通银行', '华夏银行', '民生银行', '兴业银行', '平安银行',
      '广发银行', '广大银行', '招商银行', '中信银行', '浦发银行', '邮政储蓄银行'],
    creditbanksLastIndex: 0,
    selectfile: '',
    phoneNum: '',
    username: '',
    idcard: '',
    credit: '',
    validityDate: '',
    cvn2: '',
    verificationCode: '',
    currentSelectRegionIndex: [],
    currentSelectRegion: '',

    provinces: [],
    citys: [],
    areas: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

    this.setData({ creditbanksLastIndex: this.data.creditbanks.length - 1 });
  },
  onReady: function () {
    // 页面渲染完成
    var _this = this;
    setTimeout(function () {

      if (_this.data.provinces.length <= 0) {
        var _provinces = [];
        for (var i = 0; i < 30; i++) {
          var _province = { name: '广东省' + i, citys: [] };
          var _citys = [];
          for (var j = 0; j < 10; j++) {
            var _city = { name: i + '佛山市' + j, areas: [] };
            var _areas = [];
            for (var k = 0; k < 10; k++) {
              _areas.push({ name: j + '南海区' + k });
              _city.areas = _areas;
            }
            _citys.push(_city);
            _province.citys = _citys;
          }
          _provinces.push(_province);
        }
        var _citys = _provinces[0].citys;
        var _areas = _citys[0].areas;
        _this.setData({ provinces: _provinces, citys: _citys, areas: _areas });
      }
    }, 1000);


  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onMaskClick: function (e) {

    this.setData({ isShowMask: false });
  },
  onMaskTouchMove: function () {

  },
  showMask: function (e) {
    let _id = e.currentTarget.id;
    let _tipUrl = '';
    // if ('creditcardicon' == _id) {
    //   console.log("我是信用卡");
    // } else
    if ('cvn2icon' == _id) {
      // console.log("我是cvn2");
      _tipUrl = '../../images/ic_cvn2_tips.png';
    } else if ('fileicon' == _id) {
      // console.log("我是选择文件");
       _tipUrl = '../../images/ic_bystages_pre_photo.png';
    }

    this.setData({ isShowMask: true, currentMaskId: _id, tipUrl: _tipUrl });

  },
  sendVerificationCode: function (e) {

    if ('获取验证码' != this.data.verificationCodeButtonText) {
      return;
    }
    if ('' == this.data.phoneNum) {
      _tip.toast('请输入手机号码!');
      return;
    }

    if (!_check.isPhone(this.data.phoneNum)) {
      _tip.toast('手机号码格式有误!');
      return;
    }
    this.data.isPressSendVerificationCode = true;
    var _count = 180;
    var _this = this;
    var _flag = setInterval(function () {
      if (_count < 0) {
        clearInterval(_flag);
        _this.setData({ verificationCodeButtonText: "获取验证码" });
      } else {
        _this.setData({ verificationCodeButtonText: _count + " s " });
        _count--;
      }

    }, 1000)
  },
  selectfile: function (e) {

    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        _this.setData({ selectfile: tempFilePaths[0] })
      }
    });
  },
  previewImage: function () {
    var _filePath = this.data.selectfile;
    wx.previewImage({
      // urls: ['http://img06.tooopen.com/images/20170113/tooopen_sy_196830252422.jpg'] // 需要预览的图片http链接列表
      urls: [_filePath] // 需要预览的图片http链接列表
    })
  },
  bindKeyInput: function (e) {
    let currentId = e.target.id;
    var _value = e.detail.value;
    if ('phonenum' == currentId) {
      this.data.phoneNum = _value;
    } else if ('username' == currentId) {
      this.data.username = _value;
    } else if ('idcard' == currentId) {
      this.data.idcard = _value;
    } else if ('credit' == currentId) {
      this.data.credit = _value;
    } else if ('validityDate' == currentId) {
      this.data.validityDate = _value;
    } else if ('cvn2' == currentId) {
      this.data.cvn2 = _value;
    } else if ('verificationCode' == currentId) {
      this.data.verificationCode = _value;
    }
  },
  regionChange: function (e) {
    var _val = e.detail.value;
    var _currentSelectRegionIndex = this.data.currentSelectRegionIndex;
    if (_currentSelectRegionIndex[0] != _val[0]) {
      _val[1] = 0;
      _val[2] = 0;
    } else if (_currentSelectRegionIndex[1] != _val[1]) {
      _val[2] = 0;
    }

    var _provinces = this.data.provinces[_val[0]];
    var _citys = _provinces.citys;
    var _areas = _citys[_val[1]].areas;


    this.setData({
      currentSelectRegionIndex: _val,
      currentSelectRegion: _provinces.name + _citys[_val[1]].name + _areas[_val[2]].name,
      citys: _citys,
      areas: _areas
    })

  },
  onClick: function (e) {
    let _id = e.currentTarget.id;
    if ('agreement' == _id) {
      this.setData({ isAgree: !this.data.isAgree });
      _swit.swit("../agreement/agreement");
    } else if ('applyagreement' == _id) {
      _swit.swit("../agreement/agreement");
    } else if ('finish' == _id) {
      this.setData({ isShowMask: false });
    } else if ('submit' == _id) {

      var _userName = this.data.username;
      if ('' == _userName) {
        _tip.toast('请输入持卡人姓名!');
        return;
      }

      var _idcard = this.data.idcard;
      if ('' == _idcard) {
        _tip.toast('请输入身份证号!');
        return;
      }

      if (!_check.isIdCard(_idcard)) {
        _tip.toast('身份证号输入有误!');
        return;
      }

      var _credit = this.data.credit;
      if ('' == _credit) {
        _tip.toast('请输入信用卡号!');
        return;
      }

      var _validityDate = this.data.validityDate;
      if ('' == _validityDate) {
        _tip.toast('请输入有效期!');
        return;
      }

      var _cvn2 = this.data.cvn2;
      if ('' == _cvn2) {
        _tip.toast('请输入CVN2!');
        return;
      }

      var _phone = this.data.phoneNum;
      if ('' == _phone) {
        _tip.toast('请输入手机号码!');
        return;
      }

      if (!_check.isPhone(_phone)) {
        _tip.toast('手机号码格式有误!');
        return;
      }

      if (!this.data.isPressSendVerificationCode) {
        _tip.toast('请先获取验证码!');
        return;
      }

      var _verificationCode = this.data.verificationCode;
      if ('' == _verificationCode) {
        _tip.toast('请输入验证码!');
        return;
      }

      var _selectfile = this.data.selectfile;
      if ('' == _selectfile) {
        _tip.toast('请选择文件!');
        return;
      }
      _swit.switClose("../success/success");
    }
  }
})